home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / gold / varMethods.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.3 KB  |  140 lines

  1. /*
  2.  * The original copyright owners of the accompanying source code files have
  3.  * agreed to place such code into the public domain.  Accordingly, anyone
  4.  * who receives or obtains a copy of such source code is freely entitled to
  5.  * reproduce, use and otherwise exploit such code (including the right to
  6.  * make derivative works), at his/her own risk and expense, without any
  7.  * obligation or liability to the original copyright owners.
  8.  *
  9.  * We would appreciate (but do not require) that the following message be
  10.  * included in any derivative works:
  11.  *
  12.  * "Portions of this program were developed by Peter Broadwell, Rob Myers
  13.  * and Robin Schaufler while working in Silicon Valley."
  14.  *
  15.  * The accompanying source code files and related documentation materials
  16.  * are distributed on an "AS IS" basis, without any warranties or
  17.  * guarantees of any kind.  All implied warranties, including the implied
  18.  * warranties of merchantability and of fitness for any particular purpose,
  19.  * are expressly disclaimed.
  20.  */
  21. #include "math.h"
  22. #include <gl.h>
  23. #include "geom.h"
  24. #include "class.h"
  25. #include "classIds.h"
  26. #include "selectors.h"
  27. #include "mbox.h"
  28. #include "individual.h"
  29.  
  30. #define PI 3.14159265358979323844
  31.  
  32.     /*
  33.      *  change the heading by the specified delta
  34.      */
  35. /* ARGSUSED */
  36.     inst *
  37. changeHead(self, argtype, delta)
  38.     register individual *self;
  39.     int argtype;
  40.     register point2df *delta;
  41. {
  42.     point2df carvel, polvel;
  43.  
  44.     carvel.x = self->heading.x;
  45.     carvel.y = self->heading.y;
  46.  
  47.     ctop(&polvel, &carvel);
  48.     if(polvel.x < 1e-10 && polvel.y == PI/2) {
  49.     polvel.x = 1.0;
  50.     polvel.y = 0.0;
  51.     }
  52.     polvel.y += delta->x;
  53.     ptoc(&carvel, &polvel);
  54.  
  55.     self->heading.x = carvel.x;
  56.     self->heading.y = carvel.y;
  57.     normalize(&self->heading.x);
  58.  
  59.     /*****
  60.     self->velocity.x = self->heading.x * self->speed;
  61.     self->velocity.y = self->heading.y * self->speed;
  62.     *****/
  63. }
  64.  
  65.     /*
  66.      *  change the azimuth by the specified delta
  67.      */
  68. /* ARGSUSED */
  69.     inst *
  70. changeAzimuth(self, argtype, delta)
  71.     register individual *self;
  72.     int argtype;
  73.     register point2d *delta;
  74. {
  75.     self->position.z += delta->y * 10;
  76.     self->lastPosition.z += delta->y * 9;
  77.     if(self->position.z < -4000)  {
  78.     self->position.z = self->lastPosition.z = -4000;
  79.     }
  80.     if(self->position.z > 4000) {
  81.     self->position.z = self->lastPosition.z = 4000;
  82.     }
  83. }
  84.  
  85.     /*
  86.      *  change the speed to the new value
  87.      */
  88. /* ARGSUSED */
  89.     inst *
  90. changeSpeed(self, argtype, newspeed)
  91.     register individual *self;
  92.     int argtype;
  93.     register point2df *newspeed;
  94. {
  95.     self->speed = newspeed->x;
  96.     /****
  97.     normalize(&self->heading);
  98.     self->velocity.x = self->heading.x * self->speed;
  99.     self->velocity.y = self->heading.y * self->speed;
  100.     *****/
  101. }
  102.  
  103.     /* polar to cartesian coordinates */
  104. ptoc(c,p)
  105.     point2df *c;
  106.     point2df *p;
  107. {
  108.     c->x = p->x * sin(p->y);
  109.     c->y = p->x * cos(p->y);
  110. }
  111.  
  112.     /* cartesian to polar coordinates */
  113. ctop(p,c)
  114.     point2df *p;
  115.     point2df *c;
  116. {
  117.     p->x = HYPOT(c->x, c->y);
  118.     p->y = atan2(c->x,c->y);
  119. }
  120.  
  121. #ifdef TESTING
  122. #define TESTING
  123. test_main()
  124. {
  125.     point2df p, c;
  126.  
  127.     c.x = 3;
  128.     c.y = 4;
  129.     ctop(&p,&c);
  130.     printf("%g,%g\n",p.x,p.y);
  131.     ptoc(&c,&p);
  132.     printf("%g,%g\n",c.x,c.y);
  133.     p.x = 3;
  134.     p.y = 4;
  135.     ptoc(&c,&p);
  136.     printf("%g,%g\n",c.x,c.y);
  137.  
  138. }
  139. #endif
  140.